﻿using UnityEngine;
using Zero;

public class ElementElectroChargedState : IElementState
{
    private const float Interval = 1f;
    private const float GaugeCost = 0.4f;

    private readonly IElementHolder _holder;
    private readonly IElementAura _hydroAura;
    private readonly IElementAura _electroAura;
    private float _intervalTimer;

    /// <summary>
    /// 附加源
    /// </summary>
    public GameObject Source { get; set; }

    /// <summary>
    /// 是否已启动
    /// </summary>
    public bool IsStarted { get; private set; }

    public ElementElectroChargedState(IElementHolder holder)
    {
        _holder = holder;
        _electroAura = _holder.GetElementAura("雷");
        _hydroAura = _holder.GetElementAura("水");
    }

    public void Start(GameObject source)
    {
        Source = source;
        IsStarted = true;
        _intervalTimer = Interval;
    }

    public void Stop()
    {
        IsStarted = false;
    }

    public void Update(float deltaTime)
    {
        if (IsStarted)
        {
            _intervalTimer += deltaTime;
            if (_intervalTimer >= Interval)
            {
                if (_electroAura.Gauge >= 0f && _hydroAura.Gauge >= 0f)
                {
                    _electroAura.Gauge -= GaugeCost;
                    _hydroAura.Gauge -= GaugeCost;
                    TakeDamage();
                    _intervalTimer -= Interval;
                }
            }

            if (_electroAura.Gauge <= 0f || _hydroAura.Gauge <= 0f)
            {
                Stop();
            }
        }
    }

    private void TakeDamage()
    {
        var attackData = new AttackData
        {
            Damage = 10f,
            Source = Source,
            ElementType = ElementType.Electric,
            ElementGauge = 0f,
            StrikeType = StrikeType.None
        };
        _holder.Owner.GetComponent<ActorDamageable>().TakeDamage(attackData);
    }
}